//
//  AddComViewController.m
//  Kfangchan
//
//  Created by 朋飞 on 14-11-13.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "AddComViewController.h"
#pragma mark 添加所属公司#pragma mark
@interface AddComViewController ()

@property (strong, nonatomic)  NSString *comeName;

@property (strong, nonatomic) IBOutlet UITextField *comnameTextField;
@end

@implementation AddComViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 返回
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"添加所属公司"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
    self.navigationItem.rightBarButtonItem = [[Theam currentTheam] navigationBarRightButtonItemWithImage:nil Title:@"保存" Target:self Selector:@selector(save)];
     self.comeName = [AppConfig getAgent][AGENT_company];
    //self.comnameTextField.text =[AppConfig getAgent][AGENT_company];
}

#pragma mark -
#pragma mark 关闭键盘
- (IBAction)hideKeyboard {
    [self.comnameTextField resignFirstResponder];
}



#pragma mark 保存
#pragma mark
-(void) save{
    BOOL  verify = [self verify];
    if(verify){
        //弱引用
        __weak AddComViewController *weak_self = self;
        // 构建请求参数
        NSDictionary *param = @{
                                @"company":self.comnameTextField.text
                               
                                };
        // 请求
        MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:[param mutableCopy] path:KFC_UPDATE completionHandler:^(id responseObject) {
            if(responseObject){
                id obj = [responseObject objectForKey:@"agent"];
                Agent *agent = [[Agent alloc] initWithDictionary:obj];
                NSMutableDictionary* dic = [[AppConfig getAgent] mutableCopy];
                [dic setValue:agent.company forKey:@"company"];
                [AppConfig setAgent:dic];            [weak_self back];
            }
            
        } errorHandler:^(NSError *error){
            
            showCustomAlertMessage(@"网路错误");
            
        }finishHandler:^(id responseObject) {
                       
        }];
        
        [self.opHandlers addObject:operation];
        
    }
    
    
}

-(BOOL) verify{
    NSString *comname = self.comnameTextField.text;
    
    UIAlertView *alert = nil;
    
    
    if(comname.length==0){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入公司机构码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    if([comname isEqualToString: self.comeName]){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"没有任何修改" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
        return NO;
    }

    
    //    if(comname.length<6){
    //        alert = [[UIAlertView alloc]initWithTitle:nil message:@"用户名不少于6个字符" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    //        [alert show];
    //        return NO;
    //    }
    //
    
    
    
    
    return YES ;
}

- (IBAction)call:(id)sender
{
    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"拨打电话:%@", @"4006-999-068"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles: @"确定", nil];
    [alterView show];
    
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        NSString *phone = @"4006999068";
        NSString *tel = [NSString stringWithFormat:@"tel://%@", phone];
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:tel]]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
        }
    }
}



#pragma mark 返回
#pragma mark
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
